//
//  MTBaseViewController.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/19.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTBaseViewController.h"
#import "MTSettingGroupItem.h"
#import "MTSettingRowItem.h"
#import "MTSettingCell.h"
#import "MTArrowItem.h"


@interface MTBaseViewController ()

@end

@implementation MTBaseViewController

-(instancetype)init {
    
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (NSMutableArray *)groupArray {
    
    if (_groupArray == nil) {
        _groupArray = [NSMutableArray array];
    }
    return _groupArray;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    MTSettingGroupItem *groupItem =  self.groupArray[section];
    return groupItem.rowArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTSettingCell *cell = [MTSettingCell cellWithTableView:tableView Style:UITableViewCellStyleDefault];
    MTSettingGroupItem *groupItem =  self.groupArray[indexPath.section];
    MTSettingRowItem *item = groupItem.rowArray[indexPath.row];
    cell.rowItem = item;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    MTSettingGroupItem *groupItem = self.groupArray[indexPath.section];
    MTSettingRowItem *rowItem = groupItem.rowArray[indexPath.row];
  
    if ([rowItem isKindOfClass:[MTArrowItem class]]) {

        MTArrowItem *arrowItem = (MTArrowItem *)rowItem;
        if (arrowItem.rowTask) {
            arrowItem.rowTask(indexPath);
            return ;
        }
        
        if (arrowItem.desClass) {
            UIViewController *vc = [[arrowItem.desClass alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }
}

//返回当前组的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    MTSettingGroupItem *item = self.groupArray[section];
    return item.footerTitle;
}
//返回当前组的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    MTSettingGroupItem *item = self.groupArray[section];
    return item.headerTitle;
}



@end
